package com.steven.image.service

import com.alibaba.fastjson.JSONObject
import com.steven.image.dao.ImageDao
import com.steven.image.utils.FileTypeUtils
import com.steven.image.utils.SystemUtils
import com.steven.image.utils.makeRes
import org.apache.commons.io.FileUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.util.*

@Service
class ImageServiceImp : ImageService {
    @Autowired
    var imageDao: ImageDao? = null

    override fun updateUserIcon(uid: Long, iconURL: String): JSONObject {
        val jsonObject = JSONObject()
        try {
            imageDao?.updateUserIcon(uid, iconURL)
            jsonObject["msg"] = "success"
        } catch (e: Exception) {
            e.printStackTrace()
            jsonObject["msg"] = e.message
        }
        return makeRes(0, jsonObject)
    }

    override fun queryUserIconUrl(uid: Long): JSONObject {
        val jsonObject = JSONObject()
        try {
            val list = imageDao?.queryUserIconUrl(uid)
            if (list?.size ?: 0 > 0) {
                jsonObject["data"] = list?.get(0)
            }
            jsonObject["msg"] = "success"
        } catch (e: Exception) {
            e.printStackTrace()
            jsonObject["msg"] = e.message
        }
        return makeRes(0, jsonObject)
    }

    override fun uploadIcon(image: MultipartFile, uid: Long): JSONObject {
        val jsonObject = JSONObject()
        if (!testFileIsRight(image)) {
            jsonObject["msg"] = "文件类型错误"
            return makeRes(0, jsonObject)
        }
        val imgName = uid.toString() + "_" + "icon" + "_" + Objects.requireNonNull(image.originalFilename).replace(" ".toRegex(), "")
        val imgFilePath = "${
            if (SystemUtils.isOSWin()) {
                "f:/pic/"
            } else if (SystemUtils.isOSLinux()) {
                "/root/java/icon/"
            } else {
                throw Exception("系统类型错误")
            }
        }$imgName";
        try {
            val file = FileUtils.getFile(imgFilePath)
            if (file.exists()) {
                file.delete()
            }
            val out: OutputStream = FileOutputStream(imgFilePath)
            out.write(image.bytes)
            out.flush()
            out.close()
            imageDao?.updateUserIcon(uid, imgFilePath)
            jsonObject["msg"] = "success"
        } catch (e: Exception) {
            jsonObject["msg"] = e.message
            e.printStackTrace()
        }
        return makeRes(0, jsonObject)
    }

    private fun testFileIsRight(file: MultipartFile): Boolean {
        return if (!file.isEmpty) {
            try {
                val type: String = FileTypeUtils.getFileTypeByStream(file.bytes)
                "jpg" == type || "png" == type
            } catch (e: IOException) {
                e.printStackTrace()
                false
            }
        } else {
            false
        }
    }
}